<?php

namespace App\ViewModels;

use App\Models\SessionsModel;

class CommandsViewModel extends BaseViewModel
{
    public function getCommandsQuery($server_ip, $userId, $take = 0)
    {
        return $take == 0 ? $this->getCommands($server_ip, $userId) : $this->getCommandsTake($server_ip, $userId, $take);
    }

    private function getCommands($server_ip, $userId)
    {
        return SessionsModel::query()->where([
            ['user_id', '=', $userId],
            ['server', '=', $server_ip],
        ])->first()->commands()->orderBy('time')->get();
    }

    private function getCommandsTake($server_ip, $userId, $take)
    {
        return SessionsModel::query()->where([
            ['user_id', '=', $userId],
            ['server', '=', $server_ip],
        ])->first()->commands()->orderBy('time')->take($take)->get();
    }
}
